package com.byzx.wine.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.byzx.wine.po.Purchaseordersub;
import com.byzx.wine.po.Syuser;
import com.byzx.wine.service.impl.PurchaseOrderService;
import com.byzx.wine.util.Page;

@Controller
@RequestMapping("/purchaseorder")
public class PurchaseOrderAction {
	@Autowired
	private PurchaseOrderService purchaseorderService;
	
	
	@RequestMapping("/purchase")
	public String login(HttpServletRequest request,HttpServletResponse response){
		return "Purchaseorder/PurchaseOrder";
	}
	
	@RequestMapping("findpurchaseorder")
	@ResponseBody
	public Map<String, Object> searchWine(Model model, Integer iDisplayLength,
			Integer sEcho, Integer iDisplayStart) {
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(
				iDisplayStart, iDisplayLength, map);
		Page<Map<String, Object>> page1 = purchaseorderService.findPage(page);

		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}
	
	@RequestMapping("/selectByUserid")
	@ResponseBody
	public Map<String, Object> purchaseOrderDetail(Integer userid){
		List<Map<String, Object>> list = purchaseorderService.purchaseOrderDetail(userid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		return map;
	}
	
	
	@RequestMapping("purchaseorderSelect")
	@ResponseBody
	public Map<String, Object> purchaseorderAdd() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = purchaseorderService.findAll();
		map.put("list", list);
		return map;

	}
	
	@RequestMapping("purchaseorderSelectwine")
	@ResponseBody
	public Map<String, Object> purchaseorderSelectwine(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = purchaseorderService.purchasesubSelectwine(id);
		map.put("list", list);
		return map;

	}
	
	@RequestMapping("purchaseorderadd")
	@ResponseBody
	public Map<String, Object> purchaseorderadd(String username,Integer phid,Integer wid,Integer num) {
		Map<String, Object> map = new HashMap<String, Object>();
		Syuser record = new Syuser();
		record.setName(username);
		Syuser syuser = purchaseorderService.selectByColumn(record);
		Purchaseordersub recrod = new Purchaseordersub();
		recrod.setPhid(1);
		recrod.setUid(syuser.getId());
		recrod.setWid(wid);
		recrod.setNum(num);
		recrod.setPhoid(phid);
		Integer i = purchaseorderService.insertSelective(recrod);
		map.put("i", i);
		return map;

	}
	
}
